import { Component } from '@angular/core';
import {Events, IonicPage, NavController, NavParams} from 'ionic-angular';
import {HttpService} from "../../providers/HttpService";
import {InfoProvider} from "../../providers/InfoProvider";
import {CommProvider} from "../../providers/CommProvider";

/**
 * Generated class for the QuestionPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-question',
  templateUrl: 'question.html',
})
export class QuestionPage {

    item_mark: Array<any> = [
        {
            color: 'primary',
            label: '待回复'
        },
        {
            color: 'red',
            label: '已回复'
        }
    ];
    user_info: any;
    start: number = 0;
    question_list: any[] = [];
    question_count: number = 0;

    constructor(public navCtrl: NavController, public navParams: NavParams,
                private events: Events,
                private commProvider: CommProvider,
                private infoProvider: InfoProvider,
                private httpService: HttpService) {
        this.user_info = this.infoProvider.getUserInfo();

        if (this.infoProvider.isNative()) {
          this.events.subscribe('push:qa-create', (data) => {
            console.log('push:qa-create:', data);
            if (this.question_list.length > 0) {
              this.question_list.unshift(data);
              this.start = this.question_list.length;
            } else {
              this.getQuestionList(0, false).then(res => {
                if (res.status == 0) {
                  this.question_list = res.data.rows;
                  this.start = this.question_list.length;
                }
              });
            }
          });
        }
    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad QuestionPage');
    }

    ionViewWillEnter() {
        if (this.user_info.type >= 0) {
            this.getQuestionList(0, false).then(res => {
                if (res.status == 0) {
                    this.question_list = res.data.rows;
                    this.start = this.question_list.length;
                }
            });
        }
    }

    getQuestionList(start: number = 0, loader: boolean = true): Promise<any> {
        return new Promise(resolve => {
            this.httpService.instance()
                .setUrl('/question/getQuestion')
                .setQuery({
                    user_id: this.user_info.type > 2 ? null : this.user_info.id,
                    status: 1,
                    start: start,
                    size: 10,
                    orderKey: 'read',
                    orderVal: this.user_info.type > 2 ? 'asc' : 'desc'
                })
                .showLoader(loader)
                .getRequest()
                .then(res => {
                    this.question_count = res.data.count;
                    resolve(res);
                });
        });
    }

    onRefresh(refresher) {
        if (this.user_info.type >= 0) {
            this.start = 0;
            this.getQuestionList(this.start, false).then(res => {
                if (res.status == 0) {
                    this.question_list = res.data.rows;
                    this.start = this.question_list.length;
                }
                refresher.complete();
            });
        }
    }

    onInfinite(infiniteScroll) {
        setTimeout(() => {
            this.getQuestionList(this.start, false).then(res => {
                if (res.status == 0) {
                    this.question_list = this.question_list.concat(res.data.rows);
                    this.start = this.question_list.length;
                }
                infiniteScroll.complete();
            });
        }, 200);
    }

    onItemSelected(data: any) {
        this.navCtrl.push('AnswerPage', {question: data});
    }

    onItemDeleted(data: any) {
        this.commProvider.showConfirmAlert('关闭问题', '确定要关闭该问题吗？').then(confirm => {
            if (confirm) {
                this.httpService.instance()
                    .setUrl('/question/disableQuestion/' + data.id)
                    .setBody({
                        status: 0
                    })
                    .putRequest()
                    .then(res => {
                        if (res.status == 0) {
                            this.getQuestionList().then(res => {
                                if (res.status == 0) {
                                    this.question_list = res.data.rows;
                                    this.start = this.question_list.length;
                                }
                            });
                        }
                    });
            }
        });
    }

    onNewQuestion() {
        this.navCtrl.push('NewQuestionPage');
    }

    onUserLogin() {
        this.navCtrl.push('LoginPage', {guest_login: true});
    }
}
